package com.ja.openchat.http;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lzy.okgo.callback.AbsCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;


public abstract class HttpCallback extends AbsCallback<JSONObject> {

    private AppCompatActivity mActivity;
    private Fragment mFragment;
    private boolean isShowDialog;
    private String mLoadingStr = "请稍后...";

    public HttpCallback() {
    }

    public HttpCallback(AppCompatActivity mActivity, boolean isShowDialog) {
        this.mActivity = mActivity;
        this.isShowDialog = isShowDialog;
    }

    public HttpCallback(AppCompatActivity mActivity, boolean isShowDialog, String mLoadingStr) {
        this.mActivity = mActivity;
        this.isShowDialog = isShowDialog;
        this.mLoadingStr = mLoadingStr;
    }

    public HttpCallback(Fragment mFragment, boolean isShowDialog) {
        this.mFragment = mFragment;
        this.isShowDialog = isShowDialog;

        mActivity = (AppCompatActivity) mFragment.getActivity();
    }

    public HttpCallback(Fragment mFragment, boolean isShowDialog, String mLoadingStr) {
        this.mFragment = mFragment;
        this.isShowDialog = isShowDialog;
        this.mLoadingStr = mLoadingStr;

        mActivity = (AppCompatActivity) mFragment.getActivity();
    }

    @Override
    public JSONObject convertResponse(okhttp3.Response response) {
        assert response.body() != null;
        try {
            return JSON.parseObject(response.body().string());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public void onSuccess(Response<JSONObject> response) {
        if (response == null) {
            return;
        }
        JSONObject body = response.body();
        if (body == null) {
            onError(-1, "数据解析错误");
            return;
        }
        LogUtils.eTag("OKGo", response.getRawResponse().request().url().toString() +
                "\n\n" + response.body().toString());
        /*try {*/
        ResponseBean bean = JSONObject.parseObject(response.body().toJSONString(), ResponseBean.class);
        if (bean != null) {
            switch (bean.getStatus()) {
                case 200:
                    onSuccess(bean.getStatus(), bean.getMsg(), bean.getData());
                    break;
                case 401:
//                    if (mActivity != null) AppConfig.reLogin(mActivity);

                    onError(bean.getStatus(), bean.getMsg());
                    break;
                default:
                    onError(bean.getStatus(), bean.getMsg());
                    break;
            }
        } else {
            onError(-1, "数据解析错误");
        }
        /*} catch (Exception e) {
            onError(-1, e.getMessage());
        }*/
    }

    @Override
    public void onError(Response<JSONObject> response) {
        try {
            Throwable t = response.getException();
            LogUtils.eTag("OKGo", response.getRawResponse().request().url() + "\n\n" + t.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }

//        if (isShowDialog) WaitDialog.dismiss();
        onError(-1, "网络请求错误");
    }

    public void onError(int code, String msg) {
        ToastUtils.showShort(msg);
    }

    public abstract void onSuccess(int code, String msg, String info);

    @Override
    public void onStart(Request<JSONObject, ? extends Request> request) {
        onStart();
    }

    public void onStart() {
        if (isShowDialog && mActivity != null) {

//            WaitDialog.show(mLoadingStr).setCancelable(false);
        }
    }

    @Override
    public void onFinish() {
//        if (isShowDialog) WaitDialog.dismiss();
    }

    public boolean isSuccessCode(int code) {
        return code == 200;
    }
}
